import net.es.lookup.client.RegistrationClient;
import net.es.lookup.client.SimpleLS;
import net.es.lookup.common.exception.LSClientException;
import net.es.lookup.common.exception.ParserException;
import net.es.lookup.records.ErrorRecord;
import net.es.lookup.records.Record;
import org.joda.time.DateTime;

/**
 * Created with IntelliJ IDEA.
 * User: luke
 * Date: 7/7/13
 * Time: 5:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExampleRegistrationClient {

    /*
    This example demonstrates the use of the RegistrationClient. The purpose
    of the RegistrationClient is to register one or more records to the
    Simple Lookup Service.
     */

    public static void main(String[] args) {

        String host = "localhost";
        int port = 8090;

        try {
            /*
            An instance of SimpleLS first needs to be instantiated. This will
            be passed to the RegistrationClient constructor.
            */
            SimpleLS sls = new SimpleLS(host, port);
            RegistrationClient rc = new RegistrationClient(sls);

            /*
            An instance of Record is needed to perform a registration. The
            Record instance can be passed to the RegistrationClient via the
            setRecord method.
             */
            Record record = new Record("test");
            rc.setRecord(record);

            /*
            Alternatively, the Record instance can be passed to the
            RegistrationClient in the constructor arguments.
             */
            rc = new RegistrationClient(sls, record);

            /*
            The RegistrationClient performs the registration of the specified
            Record using the register method. The method returns a new Record
            that contains the registered Record's keyValues map appended with
            URI and expiration key/value pairs. If an instance of Record is
            returned, then a successful registration has occurred.
             */
            Record registeredRecord;
            registeredRecord = rc.register();

            /*
            If the registration fails, the returned record will be an instance
            of the ErrorRecord class. This class provides means of retrieving
            both the HTTP response and response code.
             */
            String response = ((ErrorRecord) registeredRecord).getErrorMessage();
            int responseCode = ((ErrorRecord) registeredRecord).getErrorCode();

            /*
            The Record class provides means for retrieving the record state,
            expiration date, and URL of the returned Record.
             */
            String state = record.getRecordState();
            DateTime expirationDate = record.getExpires();
            String url = record.getURI();

            /*
            The Simple Lookup Service uses "lookup/records" as its default
            relative URL. If an implementation of the Simple Lookup Service
            overrides this default, the URL can be set for the
            RegistrationClient using the setRelativeURL method.
             */
            rc.setRelativeUrl("something/else");

        } catch (LSClientException e) {

            /*
            Both the SimpleLS and RegistrationClient throw LSClientExceptions.
             */
        } catch (ParserException e) {

            /*
            The RegistrationClient throws a ParserException if registration of
            an invalid Record is attempted.
             */
        }
    }
}
